home *** CD-ROM | disk | FTP | other *** search
/ 360 Degrees of: Grand Canyon National Park / 360 Degrees of: Grand Canyon National Park.iso / pc / data / krpano / textfield.as < prev    next >
Encoding:
Text File  |  2011-01-17  |  12.0 KB  |  448 lines

  1. /*
  2.     krpano textfield plugin
  3. */
  4.  
  5. package
  6. {
  7.     import flash.display.*;
  8.     import flash.events.*;
  9.     import flash.system.*;
  10.     import flash.net.*;
  11.     import flash.filters.*;
  12.     import flash.geom.*;
  13.     import flash.text.*;
  14.     import flash.utils.*;
  15.     import flash.xml.*;
  16.     import flash.ui.Mouse;
  17.  
  18.     import krpano_as3_interface;
  19.  
  20.  
  21.     [SWF(width="400", height="300", backgroundColor="#000000")]
  22.     public class textfield extends Sprite
  23.     {
  24.         // krpano as3 interface
  25.         private var krpano:krpano_as3_interface = null;
  26.  
  27.         public var pluginpath : String = null;
  28.         public var pluginobj  : Object = null;
  29.  
  30.         public var bg  : Shape     = null;
  31.         public var txt : TextField = null;
  32.  
  33.         public var txt_width  : int = 400;
  34.         public var txt_height : int = 300;
  35.  
  36.  
  37.  
  38.         public function textfield()
  39.         {
  40.             if (stage == null)
  41.             {
  42.                 this.addEventListener(Event.ADDED_TO_STAGE, startplugin);
  43.                 this.addEventListener(Event.UNLOAD,         stopplugin);
  44.             }
  45.             else
  46.             {
  47.                 // direct startup - show version info
  48.                 stage.scaleMode = StageScaleMode.NO_SCALE;
  49.                 stage.align     = StageAlign.TOP_LEFT;
  50.  
  51.                 var txt:TextField = new TextField();
  52.                 txt.textColor = 0xFFFFFF;
  53.                 txt.selectable = false;
  54.  
  55.                 txt.htmlText =    "krpano " + "1.0.8.12" + "\n\n" +
  56.                                 "<b>textfield plugin</b>"  + "\n\n" +
  57.                                 "(build " + "CUSTOM" + ")";
  58.  
  59.                 var f:TextFormat = new TextFormat();
  60.                 f.font = "_sans";
  61.                 f.size = 14;
  62.                 txt.autoSize = f.align = "center";
  63.                 txt.setTextFormat(f);
  64.  
  65.                 addChild(txt);
  66.  
  67.                 var resizefu:Function = function(event:Event):void
  68.                 {
  69.                     txt.x = (stage.stageWidth  - txt.width)/2;
  70.                     txt.y = (stage.stageHeight - txt.height)/2;
  71.                 }
  72.  
  73.                 stage.addEventListener(Event.RESIZE, resizefu);
  74.  
  75.                 resizefu(null);
  76.             }
  77.         }
  78.  
  79.  
  80.  
  81.         private function startplugin(evt:Event):void
  82.         {
  83.             this.removeEventListener(Event.ADDED_TO_STAGE, startplugin);
  84.  
  85.             if (krpano == null)
  86.             {
  87.                 // get krpano interface
  88.                 krpano = krpano_as3_interface.getInstance();
  89.  
  90.                 if ( krpano.get("version") < "1.0.8" )
  91.                 {
  92.                     krpano.call("error(textfield plugin - wrong krpano version - min. 1.0.8 needed);");
  93.                     return;
  94.                 }
  95.  
  96.                 // add krpano plugin listeners
  97.  
  98.                 // register event to get the krpano name of the plugin
  99.                 krpano.addPluginEventListener(this, krpano_as3_interface.PLUGINEVENT_REGISTER, registerEvent);
  100.  
  101.                 // resize event to set the size of the textfield
  102.                 krpano.addPluginEventListener(this, krpano_as3_interface.PLUGINEVENT_RESIZE,   resizeEvent);
  103.                 krpano.addPluginEventListener(this, krpano_as3_interface.PLUGINEVENT_UPDATE,   updateEvent);
  104.             }
  105.         }
  106.  
  107.  
  108.  
  109.         private function stopplugin(evt:Event):void
  110.         {
  111.             // remove textfield link event listener
  112.             txt.removeEventListener(TextEvent.LINK, link_event);
  113.  
  114.             // remove krpano event listeners
  115.             krpano.removePluginEventListener(this, krpano_as3_interface.PLUGINEVENT_REGISTER, registerEvent);
  116.             krpano.removePluginEventListener(this, krpano_as3_interface.PLUGINEVENT_RESIZE,   resizeEvent);
  117.             krpano.removePluginEventListener(this, krpano_as3_interface.PLUGINEVENT_UPDATE,   updateEvent);
  118.  
  119.             // remove all elements
  120.             removeChild(bg);
  121.             bg = null;
  122.  
  123.             removeChild(txt);
  124.             txt = null;
  125.  
  126.             krpano = null;
  127.         }
  128.  
  129.  
  130.  
  131.         private function registerEvent(evt:DataEvent):void
  132.         {
  133.             // register event - "evt.data" is the krpano xml name/path of the plugin (e.g. "plugin[txt1]" or "hotspot[textspot1]")
  134.  
  135.             pluginpath = evt.data;
  136.             pluginobj  = krpano.get(pluginpath);        // get the whole krpano plugin object
  137.  
  138.             // register custom attributes with their default value (note - only lowercase attributes are possible!!!)
  139.             pluginobj.registerattribute("html",            "");
  140.             pluginobj.registerattribute("css",             "");
  141.             pluginobj.registerattribute("autosize",        "none");
  142.             pluginobj.registerattribute("wordwrap",        true);
  143.             pluginobj.registerattribute("background",      true);
  144.             pluginobj.registerattribute("backgroundcolor", 0xFFFFFF);
  145.             pluginobj.registerattribute("backgroundalpha", 1.0);
  146.             pluginobj.registerattribute("border",          false);            // new in 1.0.8.12
  147.             pluginobj.registerattribute("bordercolor",     0x000000);
  148.             pluginobj.registerattribute("borderwidth",     1);
  149.             pluginobj.registerattribute("roundedge",       0);
  150.             pluginobj.registerattribute("selectable",      true);
  151.             pluginobj.registerattribute("glow",            0);
  152.             pluginobj.registerattribute("glowcolor",       0xFFFFFF);
  153.             pluginobj.registerattribute("blur",            0);
  154.             pluginobj.registerattribute("shadow",          0);
  155.             pluginobj.registerattribute("textglow",        0);
  156.             pluginobj.registerattribute("textglowcolor",   0xFFFFFF);
  157.             pluginobj.registerattribute("textblur",        0);
  158.             pluginobj.registerattribute("textshadow",      0);
  159.  
  160.             // add custom functions / link a krpano xml function to a as3 function (note - the name of the xml function must be lowercase!!!)
  161.             pluginobj.update = updateHTML;
  162.  
  163.  
  164.             // create a background shape for the textfield
  165.             bg = new Shape();
  166.  
  167.             // create the as3 textfield itself
  168.             txt = new TextField();
  169.             txt.htmlText      = "";
  170.             txt.multiline     = true;
  171.             txt.wordWrap      = true;
  172.             txt.border        = false;
  173.             txt.background    = false;
  174.             txt.condenseWhite = true;
  175.             txt.width         = txt_width;
  176.             txt.height        = txt_height;
  177.  
  178.             // textfield link event listener
  179.             txt.addEventListener(TextEvent.LINK, link_event);
  180.  
  181.             // add background and textfield
  182.             this.addChild(bg);
  183.             this.addChild(txt);
  184.  
  185.             // update the style and content of the textfield
  186.             updateSTYLE();
  187.             updateHTML();
  188.         }
  189.  
  190.  
  191.  
  192.         private function updateEvent(dataevent:DataEvent):void
  193.         {
  194.             // the update event sends the name of the changed attribute in the "dataevent.data" variable
  195.  
  196.             // do here a quick search for the changed attribute and call the corresponding update function
  197.             var changedattribute:String = "." + String( dataevent.data ) + ".";
  198.             const data_attributes :String = ".html.css.";
  199.             const style_attributes:String = ".autosize.wordwrap.background.backgroundcolor.backgroundalpha.border.bordercolor.borderwidth.roundedge.selectable.glow.blur.shadow.textglow.textblur.textshadow.";
  200.  
  201.             if ( data_attributes.indexOf(changedattribute) >= 0 )
  202.             {
  203.                 updateHTML();
  204.             }
  205.             else if ( style_attributes.indexOf(changedattribute) >= 0 )
  206.             {
  207.                 updateSTYLE();
  208.             }
  209.         }
  210.  
  211.  
  212.  
  213.         private function resizeEvent(dataevent:DataEvent):void
  214.         {
  215.             var resizesize:String = dataevent.data;        // size has the format WIDTHxHEIGHT
  216.  
  217.             var width :int;
  218.             var height:int;
  219.  
  220.             width  = parseInt(resizesize);
  221.             height = parseInt(resizesize.slice(resizesize.indexOf("x")+1));
  222.  
  223.             // set the size of the textfield
  224.             txt.width  = width;
  225.             txt.height = height;
  226.  
  227.             // save size
  228.             txt_width  = width;
  229.             txt_height = height;
  230.  
  231.             // update background shape
  232.             updateSTYLE();
  233.         }
  234.  
  235.  
  236.  
  237.         private function link_event(textevent:TextEvent):void
  238.         {
  239.             // pass the text after the "event:" link to krpano
  240.  
  241.             krpano.call( textevent.text, null, pluginobj );
  242.         }
  243.  
  244.  
  245.  
  246.         private function updateSTYLE():void
  247.         {
  248.             // pass the krpano parameters to the as3 textfield
  249.             switch ( String( pluginobj.autosize ).toLowerCase() )
  250.             {
  251.                 case "true":
  252.                 case "auto":
  253.                 case "left":      txt.autoSize = "left";
  254.                                 break;
  255.  
  256.                 case "center":    txt.autoSize = "center";
  257.                                 break;
  258.  
  259.                 case "right":     txt.autoSize = "right";
  260.                                 break;
  261.  
  262.                 case "none":
  263.                 default:          txt.autoSize = "none";
  264.                                 break;
  265.             }
  266.  
  267.             txt.wordWrap   = pluginobj.wordwrap;
  268.             txt.selectable = pluginobj.selectable;
  269.  
  270.             // update/draw the background shape
  271.             bg.alpha = pluginobj.backgroundalpha;
  272.  
  273.             bg.graphics.clear();
  274.  
  275.             // draw a background and/or border?
  276.             if (pluginobj.background || pluginobj.border)
  277.             {
  278.                 if (pluginobj.borderwidth > 0)
  279.                     bg.graphics.lineStyle(pluginobj.borderwidth, pluginobj.bordercolor);
  280.  
  281.                 if (pluginobj.background)
  282.                     bg.graphics.beginFill(pluginobj.backgroundcolor);
  283.  
  284.                 if (pluginobj.roundedge <= 0)
  285.                     bg.graphics.drawRect(0,0,txt_width,txt_height);
  286.                 else
  287.                     bg.graphics.drawRoundRect(0,0,txt_width,txt_height, pluginobj.roundedge);
  288.             }
  289.  
  290.  
  291.             // create and apply filters for the background shape
  292.             var filters:Array = new Array();
  293.  
  294.             if (pluginobj.glow > 0)
  295.             {
  296.                 // glow = glowing range
  297.                 // glowcolor = color of the glowing
  298.                 filters.push( new GlowFilter(pluginobj.glowcolor, 1.0, pluginobj.glow,pluginobj.glow) );
  299.             }
  300.  
  301.             if (pluginobj.blur > 0)
  302.             {
  303.                 // blur = blur range
  304.                 filters.push( new BlurFilter(pluginobj.blur, pluginobj.blur) );
  305.             }
  306.  
  307.             if (pluginobj.shadow > 0)
  308.             {
  309.                 // shadow = shadow range
  310.                 filters.push( new DropShadowFilter(pluginobj.shadow) );
  311.             }
  312.  
  313.             // set or remove the filters
  314.             bg.filters = filters.length > 0 ? filters : null
  315.  
  316.  
  317.             // create and apply filters for the text itself
  318.             var textfilters:Array = new Array();
  319.  
  320.             if (pluginobj.textglow > 0)
  321.             {
  322.                 // textglow = glowing range
  323.                 // textglowcolor = color of the glowing
  324.                 textfilters.push( new GlowFilter(pluginobj.textglowcolor, 1.0, pluginobj.textglow,pluginobj.textglow) );
  325.             }
  326.  
  327.             if (pluginobj.textblur > 0)
  328.             {
  329.                 // textblur = blur range
  330.                 textfilters.push( new BlurFilter(pluginobj.textblur,  pluginobj.textblur) );
  331.             }
  332.  
  333.             if (pluginobj.textshadow > 0)
  334.             {
  335.                 // textshadow = shadow range
  336.                 textfilters.push( new DropShadowFilter(pluginobj.textshadow) );
  337.             }
  338.  
  339.             // set or remove the filters
  340.             txt.filters = textfilters.length > 0 ? textfilters : null
  341.         }
  342.  
  343.  
  344.  
  345.         // string replace helper function
  346.         private function str_replace( original:String, replace_with:String, replace:String  ):String
  347.         {
  348.             var array:Array = original.split(replace_with);
  349.             return array.join(replace);
  350.         }
  351.  
  352.  
  353.         private function updateHTML():void
  354.         {
  355.             var css:StyleSheet = new StyleSheet();
  356.  
  357.             var cssdata :String = pluginobj.css;
  358.             var htmldata:String = pluginobj.html;
  359.  
  360.             if (cssdata == null || cssdata == "")
  361.             {
  362.                 txt.styleSheet = null;
  363.             }
  364.             else
  365.             {
  366.                 if (cssdata.indexOf("data:") == 0 )
  367.                 {
  368.                     // load the content of a <data> tag
  369.                     cssdata = krpano.get("data[" + cssdata.slice(5) + "].content");
  370.                 }
  371.                 else
  372.                 {
  373.                     // directly use the given css
  374.                     cssdata = unescape(cssdata);
  375.                 }
  376.  
  377.                 css.parseCSS( cssdata );
  378.                 txt.styleSheet = css;
  379.             }
  380.  
  381.             if (htmldata.indexOf("data:") == 0 )
  382.             {
  383.                 // load the content of a <data> tag
  384.                 htmldata = krpano.get("data[" + htmldata.slice(5) + "].content");
  385.             }
  386.             else
  387.             {
  388.                 // directly use the given html
  389.                 // (<> chars are not possible in a xml attribure, therefore provide the usage of [] instead)
  390.  
  391.                 // replace '[' -> '<'
  392.                 htmldata = str_replace(htmldata,"[","<");
  393.  
  394.                 // replace ']' -> '>'
  395.                 htmldata = str_replace(htmldata,"]",">");
  396.  
  397.                 htmldata = unescape(htmldata);
  398.             }
  399.  
  400.             if (htmldata == null)
  401.             {
  402.                 htmldata = "";
  403.             }
  404.  
  405.  
  406.             txt.htmlText = htmldata;
  407.  
  408.  
  409.             if (txt.autoSize != "none")
  410.             {
  411.                 // the as3 textfield autosizing is used
  412.  
  413.                 // save size
  414.                 txt_height = txt.height;
  415.  
  416.                 //krpano.set(pluginpath + ".height", txt_height);
  417.                 pluginobj.height = txt_height;
  418.  
  419.                 // the textfield unfortuntaly doesn't provide the right size immediately
  420.                 // therefore add a short delay to get the real size
  421.                 var updatetimer:Timer = new Timer(0.001,1);
  422.                 updatetimer.addEventListener("timer", updateSIZE);
  423.                 updatetimer.start();
  424.             }
  425.  
  426.             // save the current text only height also to the xml as "textheight" variable
  427.             //krpano.set(pluginpath + ".textheight", txt.textHeight);
  428.             pluginobj.textheight = txt_height;
  429.         }
  430.  
  431.  
  432.  
  433.         private function updateSIZE(te:TimerEvent=null):void
  434.         {
  435.             txt_height = txt.height;
  436.  
  437.             //krpano.set(pluginpath + ".height",     txt_height);
  438.             //krpano.set(pluginpath + ".textheight", txt.textHeight);
  439.             // update the real textfield and text sizes in krpano
  440.             pluginobj.height     = txt_height;
  441.             pluginobj.textheight = txt.textHeight;
  442.  
  443.             // update the background shape
  444.             updateSTYLE();
  445.         }
  446.     }
  447. }
  448.